#include "Loop_time_queue.h"
#include "head.h"


// 新连接进来 加入时间队列
int insert_time_queue(time_queue_t* time_queue, int fd, time_t t)
{
    LOG_MSG("新连接进来 加入时间队列");
    int subscript = t % TIMEOUT - 1;
    time_node_t* node = (time_node_t*)malloc(sizeof(time_node_t));

    node->fd = fd;
    node->t = t;
    if(time_queue->arr[subscript] == NULL)
    {
        node->next = NULL;
        time_queue->arr[subscript] = node;
    }
    else
    {
        node->next = time_queue->arr[subscript];
        time_queue->arr[subscript] = node;
    }
    LOG_MSG("新连接进来 加入时间队列");
    return 0;
}


// 插入 hash 表
int insert_time_hash(time_hash_t* hash, int fd, time_t t)
{
    LOG_MSG("插入 hash 表");
    int subscript = fd % HSAHSEED;
    time_node_t* node = (time_node_t*)malloc(sizeof(time_node_t));
    node->fd = fd;
    node->t = t;

    if(hash->arr[subscript] == NULL)
    {
        node->next = NULL;
        hash->arr[subscript] = node;
    }
    else
    {
        node = hash->arr[subscript];
        hash->arr[subscript] = node;
    }
    LOG_MSG("插入 hash 表");
    return 0;
}


// 超时剔除
int overtime_eliminate_time_queue(time_queue_t* time_queue, time_hash_t* hash, time_t t, int epoll_fd)
{
    LOG_MSG("1超时剔除");
    // 剔除时不 -1
    int subscript = t % TIMEOUT;
    if(time_queue->arr[subscript] == NULL)
        return 0;

    time_node_t* current = time_queue->arr[subscript];
    time_node_t* next = time_queue->arr[subscript]->next;
    time_queue->arr[subscript] = NULL;
    
    while(next != NULL)
    {
        eliminate_time_hash(hash, current->fd);
        epoll_del(epoll_fd, current->fd);
        close(current->fd);
        free(current);
        current = next;
        next = next->next;
    }
    eliminate_time_hash(hash, current->fd);
    epoll_del(epoll_fd, current->fd);
    close(current->fd);
    free(current);
    LOG_MSG("2超时剔除");
    return 0;
}

// 剔除
int eliminate_time_hash(time_hash_t* hash, int fd)
{
    LOG_MSG("hash剔除");
    int subscript = fd % HSAHSEED;
    time_node_t* current = hash->arr[subscript];
    time_node_t* next = hash->arr[subscript]->next;

    if(next == NULL)
    {   
        free(current);
        return 0;
    }

    while(next->fd != fd)
    {
        current = next;
        next = next->next;
    }

    // next->fd == fd
    current->next = next->next;
    free(next);
    LOG_MSG("hash剔除");
    return 0;
}

// 普通 剔除
int eliminate_time_queue(time_queue_t* time_queue, int fd, time_t t)
{
    LOG_MSG("queue剔除");
    int subscript = t % TIMEOUT;
    time_node_t* current = time_queue->arr[subscript];
    time_node_t* next = time_queue->arr[subscript]->next;
    if(next == NULL)
    {   
        // close(next->fd);
        free(current);
        return 0;
    }

    while(next->fd != fd)
    {
        current = next;
        next = next->next;
    }
    
    // next->fd == fd
    current->next = next->next;
    // close(next->fd);
    free(next);
    LOG_MSG("queue剔除");
    return 0;
}

// 查找
int find_time_hash(time_hash_t* hash, int fd, time_t* t)
{
    LOG_MSG("查找");
    int subscript = fd % HSAHSEED;
    time_node_t* current = hash->arr[subscript];

    while(current->fd != fd)
        current = current->next;
    
    // currnet->fd == fd

    *t = current->t;
    LOG_MSG("查找");

    return 0;
}

// 移动
int move_time_queue(time_queue_t* time_queue, int fd, time_t old_t, time_t new_t)
{
    LOG_MSG("移动");
    int subscript = old_t % TIMEOUT - 1;
    time_node_t* current = time_queue->arr[subscript];
    time_node_t* next = time_queue->arr[subscript]->next;

    if(next == NULL)
    {
        time_queue->arr[subscript] == NULL;
        free(current);

        //current->time = new_time;
        // 重新插入
        insert_time_queue(time_queue, fd, new_t);
        LOG_MSG("移动");
        return 0;

    }
    while(next->fd != fd)
    {
        current = next;
        next = next->next;
    }

    // next->fd == fd
    current->next = next->next;
    free(next);
    // 重新插入
    insert_time_queue(time_queue, fd, new_t);
    LOG_MSG("移动");

    return 0;
}

// 更新
int update_time_hash(time_hash_t* hash, int fd, time_t new_t)
{
    LOG_MSG("更新");
    int subscript = fd % HSAHSEED;
    time_node_t *current = hash->arr[subscript];

    while (current->fd != fd)
        current = current->next;

    // currnet->fd == fd
    printf("原时间：%ld 新时间：%ld\n", current->t, new_t);
    current->t = new_t;
    LOG_MSG("更新");

    return 0;
}
